package com.example.weathermvvm.ui.area

import android.app.ProgressDialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.ViewModelProvider
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import com.example.weathermvvm.R
import com.example.weathermvvm.databinding.ActivityWeatherBinding
import com.example.weathermvvm.databinding.FragmentChooseAreaBinding
import com.example.weathermvvm.ui.MainActivity
import com.example.weathermvvm.ui.weather.WeatherActivity
import com.example.weathermvvm.util.InjectorUtil

class ChooseAreaFragment : Fragment() {

    companion object{
        //省份标识
        const val LEVEL_PROVINCE = 0
        //城市标识
        const val LEVEL_CITY = 1
        //县标识
        const val LEVEL_COUNTY = 2
    }

    private val viewModel:ChooseAreaViewModel by viewModels{
        InjectorUtil.getChooseAreaModelFactory()
    }
    //加载进度对话框
    private var progressDialog:ProgressDialog? = null
    private lateinit var adapter:ChooseAreaAdapter

    private var _binding:FragmentChooseAreaBinding? = null
    private val binding get() = _binding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentChooseAreaBinding.inflate(inflater,container,false)
        _binding?.viewModel = this.viewModel
        return _binding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter = ChooseAreaAdapter(requireContext(),R.layout.simple_item,viewModel.dataList)
        binding!!.listView.adapter = adapter
        observe()
    }

    private fun observe(){
        viewModel.currentLevel.observe(viewLifecycleOwner) { level ->
            when (level) {
                LEVEL_PROVINCE -> {
                    binding!!.tvTitle.text = "中国"
                    binding!!.btnBack.visibility = View.GONE
                }
                LEVEL_CITY -> {
                    binding!!.tvTitle.text = viewModel.selectedProvince?.provinceName
                    binding!!.btnBack.visibility = View.VISIBLE
                }
                LEVEL_COUNTY -> {
                    binding!!.tvTitle.text = viewModel.selectedCity?.cityName
                    binding!!.btnBack.visibility = View.VISIBLE
                }
            }
        }
        viewModel.isLoading.observe(viewLifecycleOwner){ isLoading ->
            if (isLoading) showProgressDialog() else closeProgressDialog()
        }
        viewModel.dataChanged.observe(viewLifecycleOwner){
            adapter.notifyDataSetChanged()
            binding!!.listView.setSelection(0)
            closeProgressDialog()
        }
        viewModel.areaSelect.observe(viewLifecycleOwner){
            if (it && viewModel.selectedCounty !=null){
                if (activity is MainActivity){
                    Log.e("ChooseAreaFragment","MainActivity")
                    val intent = Intent(activity,WeatherActivity::class.java)
                    intent.putExtra("weather_id",viewModel.selectedCounty!!.weatherId)
                    startActivity(intent)
                    activity?.finish()
                }else if (activity is WeatherActivity){
                    val weatherActivity = activity as WeatherActivity
////                    val weatherBinding:ActivityWeatherBinding = DataBindingUtil.setContentView<ActivityWeatherBinding>(weatherActivity,R.layout.activity_weather)
//                    weatherBinding!!.drawerLayout.closeDrawers()
                    val drawerLayout =
                        weatherActivity.findViewById<DrawerLayout>(R.id.drawer_layout)
                    drawerLayout.closeDrawers()
                    weatherActivity.viewModel.weatherId = viewModel.selectedCounty!!.weatherId
                    weatherActivity.viewModel.refreshWeather()
                }
                viewModel.areaSelect.value = false
            }
        }
        if (viewModel.dataList.isEmpty()){
            viewModel.getProvinces()
        }
    }



    /**
     *显示进度对话框
     */
    private fun showProgressDialog(){
        if (progressDialog == null){
            progressDialog = ProgressDialog(activity)
            progressDialog?.setMessage("正在加载...")
            progressDialog?.setCanceledOnTouchOutside(false)
        }
        progressDialog?.show()
    }

    /**
     * 关闭对话框
     */
    private fun closeProgressDialog(){
        progressDialog?.dismiss()
    }

    /**
     * 销毁时解除绑定
     */
    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}